﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CRCCalc
{
    public partial class CRCForm : Form
    {
        public CRCForm()
        {
            InitializeComponent();
        }

        private void CalcButton_Click(object sender, EventArgs e)
        {
            string indata;
            if (checkBoxReceiving.Checked)
            {
                indata = textBoxData.Text;
                textBoxDataN.Text = textBoxData.Text.Substring(0, textBoxData.Text.Length - (textBoxGen.Text.Length - 1));
            }
            else
            {
                textBoxDataN.Text = textBoxData.Text + "".PadLeft(textBoxGen.Text.Length - 1, '0');
                indata = textBoxDataN.Text;
            }
            textBoxData.Enabled = false;
            textBoxGen.Enabled = false;
            checkBoxReceiving.Enabled = false;
            textBoxQ.Text = textBoxData.Text[0].ToString();

            string result = indata;
            textBoxOutput.AppendText(indata.PadLeft(indata.Length + 4) + "|" + textBoxGen.Text + '\n');//(indata.PadLeft(39) + '\n');
            for (int i = 0; i < indata.Length - textBoxGen.Text.Length + 1; i++)
            {
                string divider = GetDivider(textBoxQ.Text, i);
                textBoxOutput.AppendText("XOR ".PadLeft(4 + i) + divider + '\n'); //("XOR ".PadLeft(39 + i - indata.Length) + divider + '\n');

                result = XORDivision(result, divider);
                if (i < indata.Length - textBoxGen.Text.Length)
                {
                    result += indata[textBoxGen.Text.Length + i];
                    textBoxOutput.AppendText(result.PadLeft(4 + i + divider.Length) + '\n');//(result.PadLeft(44 + i - indata.Length) + '\n'); 
                    textBoxQ.Text += result[0];
                }
                else
                {
                    textBoxOutput.AppendText(result.PadLeft(4 + indata.Length) + '\n');//(result.PadLeft(39) + '\n');
                    textBoxR.Text = result;
                }
                
                
            }
            textBoxOutput.Text = textBoxQ.Text.PadLeft(indata.Length + 4) +'\n' + textBoxOutput.Text; 
            if (!checkBoxReceiving.Checked)
            {
                textBoxT.Text = textBoxData.Text + result;
            }
            textBoxT.Enabled = true;
            textBoxDataN.Enabled = true;
            textBoxR.Enabled = true;
            textBoxOutput.Enabled = true;
            textBoxQ.Enabled = true;
        }

        private string XORDivision(string str1, string str2)
        {
            string result = "";
            for (int i = 1; i < str2.Length; i++)
            {
                if (str1[i] != str2[i])
                {
                    result += "1";
                }
                else
                {
                    result += "0";
                }
            }
            return result;
        }

        private string GetDivider(string str, int i)
        {
            string divider = "";
            if (textBoxQ.Text[i] == '1')
            {
                divider = textBoxGen.Text.ToString();
            }
            else
            {
                divider = "".PadLeft(textBoxGen.Text.Length, '0');
            }
            return divider;
        }

        private void ResetButton_Click(object sender, EventArgs e)
        {
            textBoxData.Clear();
            textBoxGen.Clear();
            textBoxR.Clear();
            textBoxOutput.Clear();
            textBoxQ.Clear();
            textBoxT.Clear();
            textBoxDataN.Clear();
            textBoxDataN.Enabled = false;
            textBoxT.Enabled = false;
            textBoxData.Enabled = true;
            textBoxGen.Enabled = true;
            textBoxR.Enabled = false;
            textBoxOutput.Enabled = false;
            textBoxQ.Enabled = false;
            checkBoxReceiving.Enabled = true;
        }

        private void checkBoxReceiving_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxReceiving.Checked)
            {
                labelR.Text = "E";
                labelD.Text = "              D";
                labelData.Text = "     T";
                labelT.Hide();
                textBoxT.Hide();
            }
            else
            {
                labelR.Text = "R";
                labelD.Text = "Data * 2^n";
                labelData.Text = "Data";
                labelT.Show();
                textBoxT.Show();
            }
        }
    }
}
